"""
#!/usr/bin/python3
# coding:utf-8            
#
# Copyright (C) 2024 - 2024 Jasonakeke， Inc. All Rights Reserved 
# @Desc	   ：
# @Time    : 2024/8/15 22:20
# @Author  : Code_By_Jasonakeke
# @Email   : 2284037977@qq.com
# @File    : 02-创建ORM映射.py
# @IDE     : PyCharm
"""
from bz2 import BZ2File

from sqlalchemy import create_engine
from sqlalchemy.ext.automap import automap_base

# 数据库的配置变量
HOSTNAME = '127.0.0.1'
PORT = '3306'
DATABASE = 'test'
USERNAME = 'root'
PASSWORD = '123456'
DB_URI = 'mysql+pymysql://{}:{}@{}:{}/{}?charset=utf8mb4'.format(USERNAME, PASSWORD, HOSTNAME, PORT, DATABASE)

engine = create_engine(DB_URI)

# 自动映射
Base = automap_base()
Base.prepare(autoload_with = engine)

# 获取所有表的映射类
# 注意：自动映射的话表名和类名一样
tables = Base.classes.keys()
print(tables)

# 可以重新定义类名
Person = Base.classes.t_person
Movies = Base.classes.t_movies

# 得到当前类是属性
keys = Person.__table__.columns.keys()
print(keys)
